﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using FRW.Common.Configuration;

namespace FileRenamer
{
    public enum MessageType
    {
        DEBUG,
        LOG,
        INFORMATION,
        WARNING,
        ERROR
    }

    public class Configuration : IConfiguration
    {
        public BindingList<RegReplace> ExpressionList = new BindingList<RegReplace>();
        public BindingList<String> Extensions = new BindingList<string>();

        public bool Debug { get; set; }
        public bool Move { get; set; }
        public bool Recursive { get; set; }
        public bool MatchDestinationFolder { get; set; }
        public bool StripFolderName { get; set; }
        public bool Overwrite { get; set; }
        public bool AutoSortExpressions { get; set; }
        public string SourceDir { get; set; }
        public string DestinationDir { get; set; }
        public MessageType StatusLevel { get; set; }

        public String ExtRegExp
        {
            get
            {
                String extMatch = null;

                foreach (String st in Extensions)
                {
                    if (extMatch != null)
                    {
                        extMatch += "|";
                    }
                    extMatch += st + "$";
                }

                return extMatch;
            }
        }

        public void DefaultConfiguration()
        {
            DefaultRegExp();
            DefaultExtensions();
            DefaultPaths();
            DefaultOptions();
        }

        public void CheckConfiguration()
        {
        }

        private void DefaultRegExp()
        {
            ExpressionList.Add(new RegReplace(@"\.", " "));
            ExpressionList.Add(new RegReplace(@"^HDTV\s+?", ""));
            ExpressionList.Add(new RegReplace(@"^BBC", "BBC - "));
            ExpressionList.Add(new RegReplace(@"^Discovery", "Discovery - "));
            ExpressionList.Add(new RegReplace(@"^Discovery Ch.*? ", "Discovery - "));
            ExpressionList.Add(new RegReplace(@"^Channel4", "Channel4 - "));
            ExpressionList.Add(new RegReplace(@"^Ch4", "Channel4 - "));
            ExpressionList.Add(new RegReplace(@"^National Geographic", "National Geographic - "));
            ExpressionList.Add(new RegReplace(@"^BBC(\s|-)+Horizon [0-9]*", "BBC - Horizon - "));
            ExpressionList.Add(new RegReplace(@"Mighty Ships", "Mighty Ships -"));
            ExpressionList.Add(new RegReplace(@"(.*) x264.*", "$1"));
            ExpressionList.Add(new RegReplace(@"(.*) HDTV.*", "$1"));
            ExpressionList.Add(new RegReplace(@"(.*) PDTV.*", "$1"));
            ExpressionList.Add(new RegReplace(@"(.*) XVID.*", "$1"));
            ExpressionList.Add(new RegReplace(@"(.*) DIVX.*", "$1"));
            ExpressionList.Add(new RegReplace(@"(.*) DVB.*", "$1"));
            ExpressionList.Add(new RegReplace(@"([0-9]+)of([0-9]+)([^\.])", "$1 of $2 -$3"));
            ExpressionList.Add(new RegReplace(@"([0-9]+)of([0-9]+)", "$1 of $2"));
            ExpressionList.Add(new RegReplace(@"([0-9]+)\s*of\s*([0-9]+)\s*([^-])", "$1 of $2 - $3"));
            ExpressionList.Add(new RegReplace(@"(-|\s)+\.", "."));
            ExpressionList.Add(new RegReplace(@"-\.", "."));
            ExpressionList.Add(new RegReplace(@"-\s*-", "-"));
            ExpressionList.Add(new RegReplace(@"\s+", " "));
            ExpressionList.Add(new RegReplace(@"S\d\dE\d\d", RegReplace.RegExpType.IGNORE));
        }

        private void DefaultExtensions()
        {
            Extensions.Add("avi");
            Extensions.Add("mkv");
            Extensions.Add("mp4");
        }

        private void DefaultPaths()
        {
            DestinationDir = @"e:\shares\tv\misc";
            SourceDir = @"e:\torrents";
        }

        private void DefaultOptions()
        {
            Debug = true;
            Move = true;
            Recursive = true;
        }
    }
}
